package com.jwiki.app.rpcui.config;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;

import com.jwiki.app.rpcui.extend.ConfigObservable;
import com.jwiki.app.rpcui.util.PropertyUtils;
import com.jwiki.app.rpcui.util.ResourceUtils;

public class Configurations {

    public static final String DIR_BASE = System.getProperty("user.dir");

    public static final String DIR_WORK = FilenameUtils.concat(DIR_BASE, "work");

    public static final String DIR_API = FilenameUtils.concat(DIR_WORK, "api");

    public static final String DIR_DEPENDENCY = FilenameUtils.concat(DIR_WORK, "deps");

    public static final String DIR_TEMP = FilenameUtils.concat(DIR_BASE, "temp");

    public static final String FILE_ATTRIBUTES = FilenameUtils.concat(DIR_WORK, "attributes.xml");

    public static final String FILE_PARAMETERS = FilenameUtils.concat(DIR_WORK, "parameters.xml");

    public static final String FILE_CONFIG = FilenameUtils.concat(DIR_WORK, "config.properties");

    public static final String LOG4J_CONFIG = FilenameUtils.concat(DIR_BASE, "log4j.properties");

    public static final Image[] DEFAULT_ICON_IMAGES = new Image[] {
            ResourceUtils.getImage(Configurations.class, "/icon/icon16.png"),
            ResourceUtils.getImage(Configurations.class, "/icon/icon32.png"),
            ResourceUtils.getImage(Configurations.class, "/icon/icon48.png"),
            ResourceUtils.getImage(Configurations.class, "/icon/icon64.png"),
            ResourceUtils.getImage(Configurations.class, "/icon/icon128.png")
    };

    public static final ConfigObservable CONFIG_OBSERVER = new ConfigObservable();

    private static Font currentFont;

    private static Properties currentProperty;

    public static synchronized Properties getCurrentProperty() {
        if (currentProperty == null) {
            try {
                currentProperty = PropertyUtils.getProperty(new File(FILE_CONFIG));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return currentProperty;
    }

    public static synchronized void setCurrentProperty(Properties currentProperty) {
        try {
            PropertyUtils.saveProperty(currentProperty, "Configurations", new File(Configurations.FILE_CONFIG));
            Configurations.currentProperty = currentProperty;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void setCurrentFont(String fontData) throws Exception {
        currentFont.dispose();
        currentFont = new Font(Display.getCurrent().getSystemFont().getDevice(), new FontData(fontData));
    }

    public static Font getCurrentFont() {
        if (currentFont == null) {
            String fontData = getCurrentProperty().getProperty("app.fontData");
            if (StringUtils.isBlank(fontData)) {
                currentFont = Display.getCurrent().getSystemFont();
            } else {
                currentFont = new Font(Display.getCurrent().getSystemFont().getDevice(), new FontData(fontData));
            }
        }

        return currentFont;
    }

}
